/*
 * protocol.h
 *
 *  Created on: 2016-8-2
 *      Author: Administrator
 */

#ifndef XUSBCORE_SRC_SCSI_PROTOCOL_H_
#define XUSBCORE_SRC_SCSI_PROTOCOL_H_

#pragma pack(push)
#pragma pack(1)


/**
 * request: host to device
 */
typedef struct __commandproto__
{
    unsigned char  CH1;     ///< stx0:0x55.
    unsigned char  CH2;     ///< stx1:0xAA
    unsigned short DevID;   ///< device num：0x0001
    int            InParam; ///< input param
    unsigned short Cmd;     ///< command code
    unsigned short CheckSum;///< checksum
} TCommandProto, *PCommandProto;

/**
 * response: device to host
 */
typedef struct __ackproto__
{
    unsigned char  RH1;
    unsigned char  RH2;
    unsigned short DevID;
    int            OutParam;
    unsigned short Ack;
    unsigned short CheckSum;
} TACKProto, *PACKProto;

/**
 * data header
 */
typedef struct __dataproto__
{
    unsigned char  dh1;
    unsigned char  dh2;
    unsigned short DevID;
} TDataHeader, *PDataHeader;

#pragma pack(pop)
/*
 * error code
 */

///communication error
#define NACK_COMM_ERR         0x1001
///1:1 verify fail
#define NACK_VERIFY_FAILED    0x1002
/// bad quality
#define NACK_BAD_FINGER       0x1003
/// not support
#define NACK_IS_NOT_SUPPORTED 0x1004
/// hardware error
#define NACK_DEV_ERR          0x1005
/// invalid param
#define NACK_INVALID_PARAM    0x1006

// no finger detected
#define NACK_IS_NOT_FP        0x2001

// command excute fail
#define NACK_IS_FAIL          0x2007
// no async command
#define NACK_NO_ASY_CMD       0x2008
// async verify time out
#define NACK_ASY_TIMEOUT      0x2009
// async verify, no press finger
#define NACK_ASY_NO_FP        0x2010

/*
 * respone code
 */

/// ack ok
#define ACK_OK  0x30
/// ack error
#define ACK_ERR 0x31

/*
 * response command
 */

/// stx 0
#define ACK_CH1 0x55
/// stx 1
#define ACK_CH2 0xAA

/*
 * protocol data
 */
/// stx 0
#define DATA_DH1 0x5A
/// stx 1
#define DATA_DH2 0xA5
/// device id
#define DEV_ID   0x0001

/*
 * command
 */

/// begin
#define CMD_BEGIN   0x01
/// stop
#define CMD_END     0x02
/// extract form image
#define CMD_EXTRACT 0x03
/// feature verify
#define CMD_MATCH   0x04
/// set feature
#define CMD_SEND_TEMPLATE 0x80
/// verify by scan
#define CMD_SCAN_MATCH 0x81
///verify by scan(async)
#define CMD_SCAN_MATCH_ASY 0x85
/// get verify result
#define CMD_GET_RESULT_ASY 0X86

#define PARAM_INVALID       -1
#define PARAM_ERROR         -2
#define ACK_HEADER_ERROR    -3
#define ACK_ERROR           -4
#define DEVID_ERROR         -5
#define CHECK_SUM_ERROR     -6

#define SEND_SIZE 65536 // 64*1024

unsigned short calc_checksum(unsigned char *p, int len);
T_U32 CheckSum(unsigned char *param, T_U32 count, T_U32 bit);

int check_ack_packet(unsigned char *buffer, int buflen);
int build_command_packet(unsigned char *buffer, int buflen, unsigned char cmd, int param);

int send_data(void *handle, unsigned char *data, int data_size);
int send_data(void *handle, unsigned char *data, int size, int *OutParam, int timeOut);
int build_data_packet(unsigned char *buffer, int size, unsigned char *image);
int send_command(void *handle, unsigned char cmd, int InParam, int *OutParam);
int get_bulk_data(void *handle, unsigned char cmd, int InParam, unsigned char *buffer, int len);
int read_data(void *handle, unsigned char *data, int size, int *OutParam);

#endif /* PROTOCOL_H_ */
